package figure8.GUI;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.LayoutManager;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Locale;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JToggleButton;
import javax.swing.border.BevelBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import figure8.Controller;
import figure8.LoopTiming;
import figure8.Track;
import figure8.WAVTrack;
import figure8.GUI.EffectWindow.Effect;

public class DelayPanel extends EffectPanel {
	public enum NoteLen {Demisemiquaver, Dotted_Demisemiquaver,
						 Semiquaver,     Dotted_Semiquaver,
						 Quaver,         Dotted_Quaver,
						 Crotchet,       Dotted_Crotchet,
						 Minim,          Dotted_Minim,
						 Semibreve                              };
    final JComboBox noteLen = new JComboBox(NoteLen.values());;
	final JKnob repKnob;
	final JKnob decKnob;
	final EffectPanel effPan = this;
	final EffectWindow effWindow;

	Color BG = Color.WHITE;
	Color FG = Color.MAGENTA;
	
	public DelayPanel(EffectWindow effWin){ this(effWin, Effect.DELAY, (double)2, 0.3, 0.1 ); }

	public DelayPanel(EffectWindow effWin, Effect effType, double noteLenSet, double decSet, double repSet){
		super(effType, effWin);
		this.effWindow = effWin;
		
		JPanel background = new JPanel();
		background.setBackground(BG);
		background.setForeground(FG);

		JPanel titlePanel = new JPanel();
		titlePanel.setLayout(new GridBagLayout());
		titlePanel.setBackground(BG);
		titlePanel.setForeground(FG);

		//DELETE
		JLabel del = new JLabel("x");
		del.setForeground(FG);
		del.setFont(new Font(Font.DIALOG, Font.BOLD, 15));
		
		del.addMouseListener(new MouseListener() {
			public void mouseClicked(MouseEvent arg0) {	}
			public void mouseEntered(MouseEvent arg0) {	}
			public void mouseExited(MouseEvent arg0) { }
			public void mouseReleased(MouseEvent arg0) { }
			public void mousePressed(MouseEvent arg0) {
				effWindow.effPanelDeletion(effPan);
			}
		});
		
		//TITLE
		JLabel title = 
		new JLabel("   " + "DELAY...", JLabel.CENTER);
		title.setFont(new Font(Font.MONOSPACED,Font.BOLD, 21));
		title.setPreferredSize(new Dimension(220, 20));
		title.setForeground(FG);
		
		GridBagConstraints a = new GridBagConstraints();
		a.fill = GridBagConstraints.BOTH;
		a.anchor = a.CENTER;
		a.gridwidth = GridBagConstraints.RELATIVE;
		titlePanel.add(title, a);
		a.anchor = a.FIRST_LINE_END;
		a.ipadx = 30;
		a.gridwidth = GridBagConstraints.REMAINDER;
		titlePanel.add(del, a);

		//DELAY LENGTH COL
		
		noteLen.setFont(new Font(Font.MONOSPACED,Font.PLAIN, 11));
		noteLen.setForeground(Color.BLACK);
		noteLen.setSelectedIndex((int)noteLenSet);
		
		JPanel knobs = new JPanel();
		knobs.setLayout(new GridLayout(1,2));

		//DECAY COL
		JPanel decPanel = new JPanel();
		decPanel.setLayout(new BoxLayout(decPanel, BoxLayout.Y_AXIS));
		JLabel dec = new JLabel("DEC", JLabel.CENTER);
		dec.setFont(new Font(Font.MONOSPACED, Font.BOLD, 11));
		dec.setForeground(FG);
		decKnob = new JKnob(decSet, Color.BLACK, Color.WHITE);
		decPanel.add(dec, BorderLayout.CENTER);
		decPanel.add(decKnob, BorderLayout.CENTER);
		decPanel.setPreferredSize(new Dimension(75, 80));
		decPanel.setBackground(BG);
		
		//REPEATS COL
		JPanel repPanel = new JPanel();
		repPanel.setLayout(new BoxLayout(repPanel, BoxLayout.Y_AXIS));
		JLabel rep = new JLabel("REP", JLabel.CENTER);
		rep.setFont(new Font(Font.MONOSPACED, Font.BOLD, 11));
		rep.setForeground(FG);
		repKnob = new JKnob(repSet, Color.BLACK, Color.WHITE);
		repPanel.add(rep);
		repPanel.add(repKnob);
		repPanel.setPreferredSize(new Dimension(75, 80));
		repPanel.setBackground(BG);

		knobs.add(decPanel);
		knobs.add(repPanel);
		
		background.add(titlePanel);
		background.add(noteLen);
		background.add(knobs);
		this.add(background);
	}
	
	public double getNoteLen() { return noteLen.getSelectedIndex(); }
	
	public double getRep() { return repKnob.getPercent(); }
	
	public double getDec() { return decKnob.getPercent(); }
	
	public double getBPM() { 
		LoopTiming timing = effWindow.getLoopTiming();
		return timing.getTempo();	
	}
		
	public double[] getSettings() {
		double[] settings = {getNoteLen() , decKnob.getPercent(), repKnob.getPercent(), getBPM()};
		return settings;
	}

}

